package com.wang.scacodetest.controller.web;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.http.server.HttpServerRequest;
import cn.hutool.http.server.HttpServerResponse;
import com.wang.scacodetest.bean.Ajax;
import com.wang.scacodetest.domain.UserToken;
import com.wang.scacodetest.service.UserTokenService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;

@RequestMapping("sys/scacode")
@RestController
public class ScacodeController {
    @Autowired
    private UserTokenService userTokenService;
    @PostMapping
    public Ajax test(){
        UserToken userToken=new UserToken();
        userToken.setCreateTime(LocalDateTime.now());
        userToken.setState(0);
        userToken.setUuid(UUID.fastUUID().toString());
        userTokenService.save(userToken);
        return Ajax.success("成功");
    }

    @SneakyThrows
    @GetMapping
    public void sendCode(HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");

        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");

        String uuid=userTokenService.getUUid();
        response.setHeader("uuid",uuid);
        QrCodeUtil.generate(uuid, 300, 300,"jpeg" , response.getOutputStream());
    }
}
